package com.a3dweijing.wetouch2.Activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatSpinner;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.Util;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.AddAddressMsg;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Yxd on 2018/3/26.
 */

public class LocationAddActivity extends BaseActivity {
    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.top_text)
    TextView topText;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    @BindView(R.id.add_lodcation_name)
    EditText addLodcationName;
    @BindView(R.id.add_lodcation_province)
    AppCompatSpinner addLodcationProvince;
    @BindView(R.id.add_lodcation_city)
    AppCompatSpinner addLodcationCity;
    @BindView(R.id.add_lodcation_area)
    AppCompatSpinner addLodcationArea;
    @BindView(R.id.add_lodcation_location)
    EditText addLodcationLocation;
    @BindView(R.id.add_lodcation_tel)
    EditText addLodcationTel;
    @BindView(R.id.add_lodcation_submit)
    ImageView addLodcationSubmit;
    String province;
    String city;
    String area;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_location);
        ButterKnife.bind(this);
        initView();
        initsprinnerlistener();
        initData();
    }

    private void initsprinnerlistener() {
        addLodcationProvince.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                AddressMsg.AddressBean bean = (AddressMsg.AddressBean) adapterView.getItemAtPosition(i);
                province = bean.getArea_name();
                getaddress(bean.getId(), 2);

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        addLodcationCity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                AddressMsg.AddressBean bean = (AddressMsg.AddressBean) adapterView.getItemAtPosition(i);
                city = bean.getArea_name();
                getaddress(bean.getId(), 3);

            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        addLodcationArea.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                AddressMsg.AddressBean bean = (AddressMsg.AddressBean) adapterView.getItemAtPosition(i);
                area = bean.getArea_name();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

    private void initView() {
        topText.setText("添加收货地址");
    }

    private void initData() {
        getaddress(0, 1);
    }

    @OnClick({R.id.back, R.id.add_lodcation_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.add_lodcation_submit:
                addAddress();
                break;
        }
    }

    public static void startActivityForResult(Activity context, int what) {
        if (Util.isFastClick()) {
            Intent intent = new Intent();
            intent.setClass(context, LocationAddActivity.class);
            context.startActivityForResult(intent, what);
        }
    }

    private void getaddress(int parentid, final int type) {
        OkGo.post("http://admin.3dweijing.com/admin.php/api/shopping/linkage")
                .params("area_parent_id", parentid)
                .params("type", type)
                .tag(LocationAddActivity.this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e("--", "onSuccess: " + s);
                        AddressMsg msg = new Gson().fromJson(s, AddressMsg.class);
                        if (msg.isFlag()) {
                            if (msg.getAddress() != null && msg.getAddress().size() > 0) {
                                switch (type) {
                                    case 1:
                                        addLodcationProvince.setAdapter(new SpinnerAdapter(msg.getAddress()));
                                        break;
                                    case 2:
                                        addLodcationCity.setAdapter(new SpinnerAdapter(msg.getAddress()));
                                        break;
                                    case 3:
                                        addLodcationArea.setAdapter(new SpinnerAdapter(msg.getAddress()));
                                        break;
                                }
                            }
                        }
                    }
                });
    }

    public void addAddress() {
        String name = addLodcationName.getText().toString().trim();
        if (name == null || name.length() == 0) {
            Toast.makeText(this, "请输入姓名后再试", Toast.LENGTH_SHORT).show();
            return;
        }
        String location = addLodcationLocation.getText().toString().trim();
        if (location == null || location.length() == 0) {
            Toast.makeText(this, "请输入地址后再试", Toast.LENGTH_SHORT).show();
            return;
        }
        String tel = addLodcationTel.getText().toString().trim();
        if (tel == null || tel.length() != 11) {
            Toast.makeText(this, "请输入正确的电话号码", Toast.LENGTH_SHORT).show();
            return;
        }
        OkGo.post("http://admin.3dweijing.com/admin.php/api/shopping/addAddress")
                .tag(this)
                .params("consignee", name)
                .params("address_name", province + city + area + location)
                .params("consignee_tel", tel)
                .params("user_id", Singleton.getInstance().getUser().getUser_id())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e("--", "onSuccess: " + s);
                        AddAddressMsg msg = new Gson().fromJson(s, AddAddressMsg.class);
                        if (msg.isFlag()) {
                            Intent in = getIntent();
                            setResult(21, in);
                            finish();
                        }else {
                            Toast.makeText(LocationAddActivity.this, msg.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });

    }
}
